﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Httlgd.DataCenter.Services.Core;
using Httlgd.DataCenter.Services.Core.Common;
using Httlgd.DataCenter.Services.Common;

using Httlgd.DataCenter.Services.Sms.DataAccess.EntityFramework;
using Httlgd.DataCenter.Services.Sms.DataTransferObject;

namespace Httlgd.DataCenter.Services.Sms.DataAccess
{
    public class AttachmentDAL : DataAccessorBase<AttachmentDTO, Attachment, SermonManagementEntities>
    {
        #region Constructors
        public AttachmentDAL()
            : base()
        {

        }

        public AttachmentDAL(SermonManagementEntities context)
            : base(context)
        {

        }
        #endregion

        #region Overrided Methods
        protected override Attachment MapBusinessEntityToEntity_ScalarInner(Httlgd.DataCenter.Services.Sms.DataTransferObject.AttachmentDTO businessEntity, Attachment entity)
        {
            entity = base.MapBusinessEntityToEntity_ScalarInner(businessEntity, entity);

            entity.Sermon = this.objectContext.Sermon.Where(r => r.ID == businessEntity.SermonID).FirstOrDefault();

            if (businessEntity.FileTypeID.HasValue)
                entity.FileType = this.objectContext.FileType.Where(r => r.ID == businessEntity.FileTypeID).FirstOrDefault();

            return entity;
        }
        #endregion

        #region Public Methods
        public void UpdateAttachmentBinaryData(int attachmentID, byte[] data)
        {
            Attachment att = this.RetrieveEntity(attachmentID);
            if (att == null) return;

            att.Data = data;
            this.objectContext.SaveChanges();

        }

        public byte[] GetAttachmentBinaryData(int attachmentID, out string filename)
        {
            filename = "";

            Attachment att = this.RetrieveEntity(attachmentID);
            if (att == null)
                return null;

            filename = att.FileName;
            return att.Data;
        }
        #endregion

        #region Private funtionality methods
        #endregion
    }
}
